﻿using CXW.Core;
using CXW.Core.Sharp;
using CXW.Core.Sharp.Sda;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Test.Resources;

namespace Test.Launcher
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (AppMutex.Instance.IsRunning()) { return; }
            else { AppMutex.Instance.Run(); }

            if (Debugger.IsAttached)
            {
                Run();
            }
            else
            {
                try
                {
                    Run();
                }
                catch (Exception ex)
                {
                    try
                    {
                        HandleMainException(ex);
                    }
                    catch (Exception loadError)
                    {// HandleMainException can throw error when log4net is not found
                        MessageBox.Show(loadError.ToString(), "Critical error (Logging service defect?)");
                    }
                }
            }
        }
        static void HandleMainException(Exception ex)
        {
            LoggingService.Fatal(ex);
            try
            {
                MessageService.ShowException(ex, "Unhandled exception terminated SharpDevelop");
            }
            catch
            {
                MessageBox.Show(ex.ToString(), "Critical error (cannot use ExceptionBox)");
            }
        }
        static void Run()
        {
            ResourceRegister.Initialization();

            StartupSettings startup = new StartupSettings();
            startup.ApplicationRootPath = AppDomain.CurrentDomain.BaseDirectory;
            startup.DataDirectory = Path.Combine(startup.ApplicationRootPath, "Data");
            startup.ConfigDirectory = Path.Combine(startup.ApplicationRootPath, @"Data\Settings");
            startup.AddAddInsFromDirectory(Path.Combine(startup.ApplicationRootPath, "AddIns"));
            SharpDevelopHost host = new SharpDevelopHost(startup);

            ResourceService.LanguageChanged += (sender, e) =>
            {
                //WorkbenchSingleton.Workbench.Language = "Title";
            };
            host.BeforeRunWorkbench += (sender, e) => { };
            host.WorkbenchClosed += (sender, e) => { };
            bool a = false;
            WorkbenchLogin login = new WorkbenchLogin() { FormBorder = true };
            login.ClickOk += (form, user, pwd) => { form.DialogResult = DialogResult.OK; };
            login.ClickCancel += (form) => { form.DialogResult = DialogResult.Cancel; };
#if DEBUG
            login.SetUserPwd("111", "111");
#endif
            if (login.Run() == DialogResult.OK)
            {
                AddInTree.LoadMode = AddInTree.LoadAddinMode.AddinFile;
                AddInTree.AddSystemCodonId("SysManage");
                AddInTree.AddSystemCodonId("Exit");
                AddInTree.AddSystemCodonId("View");

                a = true;
            }

            if (a)
            {

                host.RunWorkbench();
            }
        }
    }
}
